package com.ljm.sys.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljm.common.enums.DataStatus;
import com.ljm.common.page.PageDto;
import com.ljm.security.util.UserUtil;
import com.ljm.service.BaseServiceImpl;
import com.ljm.sys.auth.dto.SysAuthRoleResDto;
import com.ljm.sys.auth.dto.UserRefDto;
import com.ljm.sys.auth.entity.SysAuthRole;
import com.ljm.sys.auth.entity.SysAuthRoleResource;
import com.ljm.sys.auth.mapper.SysAuthRoleMapper;
import com.ljm.sys.auth.service.ISysAuthRoleResourceService;
import com.ljm.sys.auth.service.ISysAuthRoleService;
import com.ljm.sys.auth.api.SysAuthRoleApi;
import com.ljm.sys.auth.dto.SysAuthRoleDto;
import com.ljm.sys.auth.service.ISysAuthUserRoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * <p>
    * 系统角色 服务实现类
    * </p>
 *
 * @author HuamingChen
 * @since 2020-06-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysAuthRoleServiceImpl extends BaseServiceImpl<SysAuthRoleMapper, SysAuthRole>
        implements ISysAuthRoleService, SysAuthRoleApi {

    @Resource
    private ISysAuthRoleResourceService sysAuthRoleResourceService;

    @Override
    public void saveData(SysAuthRoleDto dto) {
        SysAuthRole po=new SysAuthRole();
        mapperFacade.map(dto,po);
        if(dto.getId()==null){
            po.setCreater(UserUtil.getLoginName());
            po.setCreateTime(LocalDateTime.now());
            po.setStatus(DataStatus.NORMAL.getCode());
            po.setIsDelete(false);
            po.setOrgId(UserUtil.getCurUser().getOrgId());
            this.save(po);
        }else {
            po.setEditor(UserUtil.getLoginName());
            po.setEditTime(LocalDateTime.now());
            this.updateById(po);
        }
    }

    @Override
    public void delData(Long id) {
        this.removeById(id);
    }

    @Override
    public SysAuthRoleDto getDataById(Long id) {
        SysAuthRole po = this.getById(id);
        if(po!=null){
            SysAuthRoleDto dto=new SysAuthRoleDto();
            mapperFacade.map(po,dto);
            return dto;
        }else{
            return null;
        }
    }

    @Override
    public PageDto<SysAuthRoleDto> getDataByPage(String roleCode, String rodeName, int pageNo, int pageSize) {
        PageDto<SysAuthRoleDto> pageDto=new PageDto<>(pageNo,pageSize);
        IPage<SysAuthRole> page=new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<SysAuthRole> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(roleCode)){
            wrapper.eq(SysAuthRole::getRoleCode,roleCode);
        }
        if(StringUtils.isNotEmpty(rodeName)){
            wrapper.eq(SysAuthRole::getRoleName,rodeName);
        }
        page=this.page(page,wrapper);
        if(page.getTotal()>0){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(mapperFacade.mapAsList(page.getRecords(),SysAuthRoleDto.class));
        }
        return pageDto;
    }

    @Override
    public void setRoleRes(SysAuthRoleResDto dto) {
        sysAuthRoleResourceService.remove(new LambdaQueryWrapper<SysAuthRoleResource>().eq(SysAuthRoleResource::getRoleCode,dto.getRoleCode()));
        List<SysAuthRoleResource> list=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(dto.getResIds())){
            dto.getResIds().forEach(resId->{
                SysAuthRoleResource sysAuthRoleResource=new SysAuthRoleResource();
                sysAuthRoleResource.setCreater(UserUtil.getLoginName());
                sysAuthRoleResource.setCreateTime(LocalDateTime.now());
                sysAuthRoleResource.setOrgId(UserUtil.getCurUser().getOrgId());
                sysAuthRoleResource.setRoleCode(dto.getRoleCode());
                sysAuthRoleResource.setResId(resId);
                list.add(sysAuthRoleResource);
            });
            sysAuthRoleResourceService.saveBatch(list);
        }
    }

    @Override
    public Set<Long> getRoleRes(String roleCode) {
        List<SysAuthRoleResource> list = sysAuthRoleResourceService.list(new LambdaQueryWrapper<SysAuthRoleResource>().eq(SysAuthRoleResource::getRoleCode, roleCode));
        if(CollectionUtils.isNotEmpty(list)){
            return list.stream().map(roleRes->roleRes.getResId()).collect(Collectors.toSet());
        }else{
            return Collections.emptySet();
        }
    }

    @Override
    public Map<Class, Class> classMap() {
        Map<Class, Class> map=new HashMap<>();
        map.put(SysAuthRole.class,SysAuthRoleDto.class);
        return map;
    }
}
